<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/10/28
 * Time: 14:42
 */
namespace Cron\Model;
use Think\Model;
use Trade\Model\TradeModel;
use Push\Controller\ApiController;
use Push\Controller\PushController;

class CronModel extends Model {
    protected $tableName 		= 'group';
    protected $pk		 		= 'id';

    function _initialize(){

    }

    function expireRedbag(){
        $map['status']=array('eq',1);
        $map['deadline']=array('lt',NOW_TIME);
        $result=M('redbag')->where($map)->select();

        if(!empty($result)){
            foreach ($result as $k => $v) {
                M('redbag')->where('id='.$v['id'])->setField('status',0);
                if($v['score']!=$v['give_score']){
                    $recover_score=$v['score']-$v['give_score'];
                    M('user')->where('uid='.$v['uid'])->setInc('score',$recover_score);
                    $tradeModel=new TradeModel();
                    $tradeModel::add($v['uid'],0,$recover_score,5);

                    $content=array('title'=>'红包过期退款','list'=>array(),'type'=>'7','score'=>$recover_score,'message'=>'你有一笔红包过期退款，'.$recover_score.'元，已自动转入您的账号余额');
                    $content=json_encode($content);
                    $push=new ApiController();
                    $push::sendMsgToUser($v['uid'],$content);
                }
            }
        }
    }

    function setTimePush(){
        $map['status']=array('eq',1);//未领取完的红包
        $map['is_push']=array('eq',0);//没有发过红包结果推送
        $result=M('redbag')->where($map)->select();
        foreach ($result as $k=>$v){
            $push_time = M('session')->where(array('id'=>$v['sessionid']))->getField('push_time');
            if(NOW_TIME > ($v['create_time']+$push_time)){
                //推送给群主，红包领取的详情
                $push = new ApiController();

                $push= new ApiController();
                $return = $push::sendRedbagResult($v['id']);
                if($return){
                    M('redbag')->where(array('id'=>$v['id']))->setField('is_push',1);
                }
            }
        }
    }
}